let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId:0,
    couponStoreGoods:{},
    writingText:'',
    goodsImg:[],
    thumb:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.goodsId??'') !== '') {
      this.setData({
        goodsId: parseInt(options.goodsId)
      })
      this.getGoodsDetails(this.data.goodsId);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getGoodsDetails(id) {
    let urlStr = app.getNetAddresss("goods.goods.get-goods-page");
    app._postNetWork({
      url: urlStr,
      data: { id: Number(id) },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            couponStoreGoods: res.data.plugin?.coupon_store_goods,
            goodsImg: res.data.get_goods?.thumb_url,
            thumb: res.data.get_goods?.thumb
          })
        } else {

        }
       
      },
    });
  },
  saveImg() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              console.log('授权成功');
            }
          });
        }
      }
    });
    var imgSrc = this.data.posterImg;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 2000
            });
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限成功,再次长按保存图片',
                      duration: 1500
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限失败,保存图片需要用户授权',
                      duration: 1500
                    });
                  }
                }
              });
            }
          },
          complete(res) {
            console.log(res);
          }
        });
      }
    });
  },
  copyBtn(event) {
    let text = event.currentTarget.dataset.text;
    // 下方为微信开发文档中的复制 API
    wx.setClipboardData({
      data: text, //复制的数据
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功',
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})